<template>
	<view class="page">
		<u--form labelPosition="left" labelWidth="230rpx" ref="uForm" :model="form" :rules='formRules'>
			<u-gap height="32rpx" bgColor="transparent"></u-gap>
			<view class="form-card-title">患者信息</view>
			<view class="card-container">
				<u-cell-group :border="false">
					<u-cell title="姓名" :value="userInfo.memberName"></u-cell>
					<u-cell title="性别" :value="userInfo.memberGender==1?'男':'女'"></u-cell>
					<u-cell title="身份证" :value="userInfo.memberIdcard"></u-cell>
					<u-cell title="年龄" :value="userInfo.memberAge"></u-cell>
					<u-cell title="主诉" :value="userInfo.mainSuit"></u-cell>
					<u-cell title="出院诊断" :value="userInfo.assessmentContent"></u-cell>
					<u-cell title="随访要求" :value="userInfo.followUpRequire"></u-cell>
					<u-cell title="随访要求次数" :value="userInfo.totalTimes"></u-cell>
					<u-cell title="当前随访次数" :value="userInfo.currentTimes" :border="false"></u-cell>
				</u-cell-group>
			</view>
			<u-gap height="32rpx" bgColor="transparent"></u-gap>
			<view class="form-card-title">填报信息</view>
			<view class="card-container">
				<u-form-item label="收缩压(mmHg)" prop="patientSBP" borderBottom>
					<u--input v-model="userInfo.patientSBP" border="none" type='number' placeholder="请填写血压"
						:disabled='followUpRecordStatus===1'>
						<u--text text="mmHg" slot="suffix" margin="0 3px 0 0" type="tips"></u--text>
					</u--input>
					<u-icon v-show="userInfo.patientSBP>139" slot="right" name="arrow-upward" color="red"></u-icon>
					<u-icon v-show="userInfo.patientSBP!==''&&userInfo.patientSBP<90?true:false" slot="right"
						name="arrow-downward" color="red"></u-icon>
				</u-form-item>
				<u-form-item label="舒张压(mmHg)" prop="patientDBP" borderBottom>
					<u--input v-model="userInfo.patientDBP" border="none" type='number' placeholder="请填写血压"
						:disabled='followUpRecordStatus===1'>
						<u--text text="mmHg" slot="suffix" margin="0 3px 0 0" type="tips"></u--text>
					</u--input>
					<u-icon v-show="userInfo.patientDBP>80" slot="right" name="arrow-upward" color="red"></u-icon>
					<u-icon v-show="userInfo.patientDBP!==''&&userInfo.patientDBP<60?true:false" slot="right"
						name="arrow-downward" color="red"></u-icon>
				</u-form-item>
				<u-form-item label="体温(℃)" prop="patientBodyTemperature" borderBottom>
					<u--input v-model="userInfo.patientBodyTemperature" border="none" type='number' placeholder="请填写体温"
						:disabled='followUpRecordStatus===1'>
						<u--text text="℃" slot="suffix" margin="0 3px 0 0" type="tips"></u--text>
					</u--input>
					<u-icon v-show="userInfo.patientBodyTemperature>37" slot="right" name="arrow-upward"
						color="red"></u-icon>
					<u-icon v-show="userInfo.patientBodyTemperature!==''&&userInfo.patientBodyTemperature<36?true:false"
						slot="right" name="arrow-downward" color="red"></u-icon>
				</u-form-item>
				<u-form-item label="脉搏(次/分)" prop="patientPulse;" borderBottom>
					<u--input v-model="userInfo.patientPulse" border="none" type='number' placeholder="请填写脉搏次数"
						:disabled='followUpRecordStatus===1'>
						<u--text text="次/分" slot="suffix" margin="0 3px 0 0" type="tips"></u--text>
					</u--input>
					<u-icon v-show="userInfo.patientPulse>100" slot="right" name="arrow-upward" color="red"></u-icon>
					<u-icon v-show="userInfo.patientPulse!==''&&userInfo.patientPulse<60?true:false" slot="right"
						name="arrow-downward" color="red"></u-icon>
				</u-form-item>
				<u-form-item label="呼吸(次/分)" prop="patientBreathe" borderBottom>
					<u--input v-model="userInfo.patientBreathe" border="none" type='number' placeholder="请填写呼吸次数"
						:disabled='followUpRecordStatus===1'>
						<u--text text="次/分" slot="suffix" margin="0 3px 0 0" type="tips"></u--text>
					</u--input>
					<u-icon v-show="userInfo.patientBreathe>20" slot="right" name="arrow-upward" color="red"></u-icon>
					<u-icon v-show="userInfo.patientBreathe!==''&&userInfo.patientBreathe<12?true:false" slot="right"
						name="arrow-downward" color="red"></u-icon>
				</u-form-item>
				<u-form-item label="本次病情描述" prop="patientHeath" borderBottom>
					<u--textarea v-model="userInfo.patientHeath" placeholder="请输入内容"
						:disabled='followUpRecordStatus===1'></u--textarea>
				</u-form-item>
				<u-form-item label="备注" prop="notes" borderBottom>
					<u--textarea v-model="userInfo.notes" placeholder="请输入内容"
						:disabled='followUpRecordStatus===1'></u--textarea>
				</u-form-item>
				<u-form-item label="现场照片" prop="followUpPhoto" borderBottom>
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
						:maxCount="10"></u-upload>
				</u-form-item>
			</view>
		</u--form>
		<view class="button-group-list" v-if="followUpRecordStatus===0">
			<view class="save-button">
				<u-button type="primary" text="暂存" shape="circle" @click="onSubmit(0)"></u-button>
			</view>
			<view class="save-entry-button">
				<u-button type="warning" text="提交" shape="circle" @click="onSubmit(1)"></u-button>
			</view>
		</view>
		<view style="width: 55%; margin: 16px auto;font-size: 13px;color: #5a5a5a;">
			随访日期：{{userInfo.updateTime}}
		</view>
		<!-- 随访日期选择 -->
		<u-calendar showLunar :show="followUpTimeShow" @confirm="followUpTimeConfirm" @close='followUpTimeShow=false'
			showLunar closeOnClickOverlay>
		</u-calendar>
	</view>
</template>

<script>
	import config from '@/common/config'
	import {
		followUpDetail
	} from '@/common/api/followup/detail/detail.js'
	import {
		saveFollowUp
	} from '@/common/api/followup/add/add.js'
	import {
		imgUp,
		imgGet
	} from '@/util/utils.js'
	export default {
		onLoad(query) {
			this.query.id = query.id
			this.getDetail()
		},

		data() {
			return {
				query: {
					id: ''
				},
				followUpTimeShow: false,
				fileList1: [],
				followUpRecordStatus: '',
				formRules: {},
				form: {
					id: '',
					transferId: '',
					followUpTime: "",
					patientBodyTemperature: '',
					patientPulse: '',
					patientBreathe: '',
					patientSBP: '',
					patientDBP: '',
					patientHeath: "",
					followUpPhoto: "",
					notes: ""
				},
				userInfo: {},
				fileList2: [],
				followUpPhoto:''
			}
		},
		methods: {
			followUpTimeConfirm(e) {
				this.followUpTimeShow = false
				this.userInfo.followUpTime = e[0]
			},
			getDetail() {
				followUpDetail({
					id: this.query.id
				}).then((response) => {
					this.userInfo = response.object;
					this.followUpRecordStatus = response.object.followUpRecordStatus;
					if (response.object && response.object.followUpPhoto) {
						// const trimmedStr = response.object.followUpPhoto.replace(/^,/, '')
						// 按逗号分割字符串为数组
						const parts = response.object.followUpPhoto.split(',');
						// 将数组中的每个元素转换为对象
						const result = parts.map(item => ({ url: config.imgURl+item }));
						result.forEach((item) => {
							this.fileList1.push(item);
						});
					} else {
						this.fileList1 = [];
					}

				})
			},
			// 提交/暂存
			onSubmit(val) {
				const obj = {
					id: this.userInfo.id,
					transferId: this.userInfo.transferId,
					followUpTime: this.userInfo.followUpTime,
					patientBodyTemperature: this.userInfo.patientBodyTemperature,
					patientPulse: this.userInfo.patientPulse,
					patientBreathe: this.userInfo.patientBreathe,
					patientSBP: this.userInfo.patientSBP,
					patientDBP: this.userInfo.patientDBP,
					patientHeath: this.userInfo.patientHeath,
					followUpPhoto: this.followUpPhoto,
					notes: this.userInfo.notes
				}
				this.fileList1.forEach(item => {
					this.followUpPhoto+= ','+item.url.replace(config.imgURl,'')
				});
				obj.followUpPhoto = this.followUpPhoto.replace(/^,/, '')
				obj.transferId = this.userInfo.transferId
				const params = {
					num: val,
					form: obj
				}
				saveFollowUp(params).then(res => {
					if (res.code === 1) {
						uni.showToast({
							title: '提交成功',
							icon: 'success'
						});
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 1000);
					}
				})

			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "成功",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: "/ygt-api/file/addFile",
						filePath: url,
						name: "file",
						success: (res) => {
							const data = JSON.parse(res.data)
							resolve(config.imgURl + data.message);

						},
					});
				});
			},
		}
	}
</script>

<style lang="scss">
	.page {
		padding: 0 0 calc(64rpx + 120rpx + env(safe-area-inset-bottom)) 0;
		box-sizing: border-box;
	}

	.form-card-title {
		padding: 0 42rpx 16rpx;
		font-size: $ft-size-md;
		color: $text-desc-color;
	}

	.card-container {
		padding: 0 20rpx;
	}

	.button-group-list {
		position: fixed;
		left: 32rpx;
		bottom: calc(32rpx + env(safe-area-inset-bottom));
		display: flex;
		align-items: center;
		justify-content: space-around;
		box-sizing: border-box;
		padding: 0 10rpx;
		width: 686rpx;
		height: 100rpx;
		border-radius: 1000rpx;
		background-color: #fff;
		box-shadow: 0 0 15rpx 0 rgba(0, 0, 0, 0.05);
		z-index: 10;

		.save-button {
			width: 266rpx;
		}

		.save-entry-button {
			width: 266rpx;
		}
	}
</style>